/*
* pps_bom
*/
package com.pitop.project.service;

import com.pitop.core.entity.Page;
import com.pitop.core.exceptions.BaseException;
import com.pitop.core.exceptions.ProjectCategoryException;
import com.pitop.project.api.ProjectCategoryFeignApi;
import com.pitop.project.entity.ProjectCategory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * 项目分类
 *
 * @author berton
 */
@Service("projectCategorySVImpl")
@Slf4j
public class ProjectCategorySVImpl {

    @Autowired
    private ProjectCategoryFeignApi projectCategoryFeignApi;

    /**
     * 加载一个对象ProjectCategory
     *
     * @param id id* @param code 编码
     * @return ProjectCategory
     */
    public ProjectCategory load(Long id, String code) {
        if (id == null && code == null) {
            throw new ProjectCategoryException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        return projectCategoryFeignApi.load(id, code);
    }

    /**
     * 加载一个对象ProjectCategory 通过id,(将查询关联数据)
     *
     * @param id id
     * @return ProjectCategory
     */
    public ProjectCategory loadById(Long id) {
        if (id == null) {
            throw new ProjectCategoryException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        return projectCategoryFeignApi.loadById(id);
    }

    /**
     * 加载一个对象ProjectCategory 通过code,(将查询关联数据)
     *
     * @param code 编码
     * @return ProjectCategory
     */
    public ProjectCategory loadByCode(String code) {
        if (code == null) {
            throw new ProjectCategoryException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        return projectCategoryFeignApi.loadByCode(code);
    }


    /**
     * 删除对象ProjectCategory
     *
     * @param id id* @param code 编码
     */

    public void delete(Long id, String code) {
        if (id == null && code == null) {
            throw new ProjectCategoryException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        projectCategoryFeignApi.delete(id, code);
    }

    /**
     * 查询ProjectCategory分页
     *
     * @param projectCategory 对象
     * @param curPage         当前页
     * @param pageSize        分页条数
     * @return List<ProjectCategory>
     */
    public List<ProjectCategory> list(ProjectCategory projectCategory, int curPage, int pageSize) {
        return projectCategoryFeignApi.list(projectCategory, curPage, pageSize);
    }

    /**
     * 查询ProjectCategory分页
     *
     * @param id       id* @param code  编码
     * @param curPage  当前页
     * @param pageSize 分页条数
     * @return List<ProjectCategory>
     */
    public List<ProjectCategory> list(Long id, String code, int curPage, int pageSize) {
        if (curPage < 0) {
            curPage = 1;
        }

        if (pageSize < 0) {
            pageSize = Page.limit;
        }

        ProjectCategory projectCategory = new ProjectCategory();
        projectCategory.setId(id);
        projectCategory.setCode(code);

        return projectCategoryFeignApi.list(projectCategory, curPage, pageSize);
    }

    public int count(Long id, String code) {
        return projectCategoryFeignApi.count(id, code);
    }

    public int count(ProjectCategory projectCategory) {
        return projectCategoryFeignApi.count(projectCategory);
    }

    /**
     * 保存
     *
     * @param projectCategory 实体
     * @throws BaseException
     */
    public void save(ProjectCategory projectCategory) {
        if (projectCategory == null) {
            throw new ProjectCategoryException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        ProjectCategory projectCategoryFlag = projectCategoryFeignApi.loadByName(projectCategory.getName());
        if (projectCategoryFlag != null) {
            throw new ProjectCategoryException(BaseException.BaseExceptionEnum.Project_Category_name_Is_Exist);
        }
        projectCategoryFeignApi.build(projectCategory);
    }

    /**
     * 更新
     *
     * @param projectCategory 实体
     * @throws BaseException
     */
    public void modify(ProjectCategory projectCategory) {
        if (projectCategory == null) {
            throw new ProjectCategoryException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        ProjectCategory projectCategoryFlag = projectCategoryFeignApi.loadByName(projectCategory.getName());
        if (projectCategoryFlag != null && !projectCategory.getCode().equals(projectCategoryFlag.getCode())) {
            throw new ProjectCategoryException(BaseException.BaseExceptionEnum.Project_Category_name_Is_Exist);
        }
        projectCategoryFeignApi.modify(projectCategory);
    }
}
